<template>
  <div class="post-view-container">
    <!-- 发帖按钮 -->
    <div class="header">
      <a-space>
        <a-button type="primary" @click="handleCreate" size="large">
          <icon-plus />
          发帖
        </a-button>
        <a-button @click="showMyFavourites" size="large">
          <icon-star />
          我的收藏
        </a-button>
      </a-space>
    </div>

    <!-- 帖子卡片列表 -->
    <div class="post-list">
      <a-card
        v-for="post in data"
        :key="post.id"
        class="post-card"
        hoverable
        @click="handleView(post.id)"
      >
        <h3>{{ post.title }}</h3>
        <div class="meta">
          <span @click.stop="handleThumb(post.id)">
            <icon-thumb-up /> {{ post.thumbNum }}
          </span>
          <span @click.stop="handleFavour(post.id)">
            <icon-star /> {{ post.favourNum }}
          </span>
          <span>{{ post.createTime }}</span>
        </div>
        <p class="content">{{ post.content }}</p>
        <div v-if="post.tags?.length" class="tags">
          <a-tag v-for="tag in post.tags" :key="tag">{{ tag }}</a-tag>
        </div>
      </a-card>
    </div>

    <!-- 简单分页 -->
    <a-pagination
      v-model:current="pagination.current"
      :total="pagination.total"
      :page-size="pagination.pageSize"
      @change="loadData"
      class="pagination"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  PostControllerService,
  PostFavourControllerService,
  PostThumbControllerService,
} from "../../../generated";
import { useStore } from "vuex";

const route = useRoute();
const router = useRouter();
const store = useStore();

const postId = ref(route.params.id);
const post = ref({
  title: "",
  content: "",
  tags: [],
  thumbNum: 0,
  favourNum: 0,
  userName: "",
  createTime: "",
  updateTime: "",
});
// 数据与分页
const data = ref([]);
const pagination = ref({
  current: 1,
  pageSize: 8,
  total: 0,
});
// 添加点赞和收藏的处理方法
import { Message } from "@arco-design/web-vue";

// 点赞处理
const handleThumb = async (postId) => {
  try {
    const res = await PostThumbControllerService.doThumbUsingPost({
      postId: postId,
    });
    if (res.code === 0) {
      Message.success(res.message || "点赞成功");
      // 更新当前帖子的点赞数
      const post = data.value.find((item) => item.id === postId);
      if (post) {
        post.thumbNum += res.data;
      }
    } else {
      Message.error(res.message || "点赞失败");
    }
  } catch (e) {
    console.error("点赞出错:", e);
    Message.error("点赞失败");
  }
};
// 收藏

// 收藏处理
const handleFavour = async (postId) => {
  try {
    const res = await PostFavourControllerService.doPostFavourUsingPost({
      postId: postId,
    });
    if (res.code === 0) {
      Message.success(res.message || "收藏成功");
      // 更新当前帖子的收藏数
      const post = data.value.find((item) => item.id === postId);
      if (post) {
        post.favourNum += res.data; // 使用API返回的更新值
      }
    } else {
      Message.error(res.message || "收藏失败");
    }
  } catch (e) {
    console.error("收藏出错:", e);
    Message.error("收藏失败");
  }
};
// 加载帖子数据
const loadData = async () => {
  try {
    const res = await PostControllerService.listPostVoByPageUsingPost({
      current: pagination.value.current,
      pageSize: pagination.value.pageSize,
    });
    if (res.code === 0) {
      data.value = res.data.records.map((item) => ({
        ...item,
        tags: item.tagList || [], // 统一使用tags字段
      }));
      pagination.value.total = res.data.total;
    }
  } catch (e) {
    console.error("加载失败:", e);
  }
};
// 显示我的收藏
const showMyFavourites = async () => {
  try {
    const res =
      await PostFavourControllerService.listMyFavourPostByPageUsingPost({
        current: 1,
        pageSize: pagination.value.pageSize,
      });
    if (res.code === 0) {
      data.value = res.data.records.map((item) => ({
        ...item,
        tags: item.tagList || [],
      }));
      pagination.value = {
        current: 1,
        pageSize: pagination.value.pageSize,
        total: res.data.total,
      };
    }
  } catch (e) {
    console.error("加载收藏失败:", e);
  }
};
// 跳转发帖页
const handleCreate = () => router.push("/post/create");

// 查看帖子详情
const handleView = (id) => {
  // 添加 nextTick 确保当前渲染周期完成
  nextTick(() => {
    router.push(`/post/detail/${id}`);
  });
};

// 初始化加载
onMounted(loadData);
</script>

<style scoped>
.post-view-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  margin-bottom: 20px;
  text-align: right;
}

.post-list {
  display: grid;
  gap: 16px;
}

.post-card {
  padding: 16px;
  cursor: pointer;
  transition: transform 0.2s;
}

.post-card:hover {
  transform: translateY(-2px);
}

.post-card h3 {
  margin-bottom: 8px;
  color: var(--color-text-1);
}

.meta {
  display: flex;
  gap: 12px;
  color: var(--color-text-3);
  font-size: 13px;
  margin-bottom: 12px;
}

.content {
  color: var(--color-text-2);
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.pagination {
  margin-top: 24px;
  display: flex;
  justify-content: center;
}

.meta span {
  cursor: pointer;
  transition: color 0.2s;
}
</style>
